﻿using System;
using System.IO;
using System.IO.IsolatedStorage;

namespace BaoVeThanhDia.FileSetting
{
    class FileOption
    {
        bool isNen, isHieuUng, isRung;
        static IsolatedStorageFile storage;
        string temp;
        public FileOption()
        {
            getData();
        }
        private void getData()
        {
            storage = IsolatedStorageFile.GetUserStoreForApplication();
            if (!storage.FileExists("bvtd_Setting.txt"))
            {
                using (var writer = new StreamWriter(
                new IsolatedStorageFileStream("bvtd_Setting.txt", FileMode.Create, FileAccess.Write, storage)))
                {
                    writer.WriteLine("true;true;true");
                    writer.Flush();
                    writer.Close();
                    writer.Dispose();
                }
            }
            IsolatedStorageFileStream f = storage.OpenFile("bvtd_Setting.txt", FileMode.Open, FileAccess.Read, FileShare.Read);
            using (StreamReader r = new StreamReader(f))
            {
                do
                {
                    temp = r.ReadLine();
                    string[] result = temp.Split(';');
                    isNen = Convert.ToBoolean(result[0]);
                    isHieuUng = Convert.ToBoolean(result[1]);
                    isRung = Convert.ToBoolean(result[2]);
                } while (!r.EndOfStream);
                r.Close();
                r.Dispose();
            }//mở file ra để đọc
            storage.Dispose();
        }
        public void SaveFile(bool isnen, bool ishieuung, bool isrung)
        {
            isNen = isnen;
            isHieuUng = ishieuung;
            isRung = isrung;
            storage = IsolatedStorageFile.GetUserStoreForApplication();
            if (storage.FileExists("bvtd_Setting.txt"))
            {
                storage.DeleteFile("bvtd_Setting.txt");
            }
            using (var writer = new StreamWriter(
                new IsolatedStorageFileStream("bvtd_Setting.txt", FileMode.Create, FileAccess.Write, storage)))
            {
                writer.WriteLine(isNen.ToString() + ";" + isHieuUng.ToString() + ";" + isRung.ToString());
                writer.Flush();
                writer.Close();
                writer.Dispose();
            }
            storage.Dispose();
        }
        public void getSetting(ref bool isnen, ref bool ishieuung, ref bool isrung)
        {
            isnen = isNen;
            ishieuung = isHieuUng;
            isrung = isRung;
        }
    }
}
